package com.itheima.service.impl;

import com.itheima.admin.mapper.EmployeeMapper;
import com.itheima.admin.pojo.Employee;
import com.itheima.admin.pojo.Page;
import com.itheima.common.R;
import com.itheima.service.EmployeeService;
import com.itheima.util.JwtUtil;
import com.itheima.util.PageUitl;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author longbosheng
 * @Date 2022/6/11 14:19
 * @PackageName:com.itheima.service
 * @ClassName: EmployeeLoginService
 * @Description: TODO
 * @Version 1.0
 */
@Log4j2
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    /**
     * 员工登录
     *
     * @param employee
     * @return
     */
    @Override
    public R login(Employee employee) {
        Employee result = employeeMapper.selectStatus(employee);
        if (result != null && result.getStatus() == 0) {
            log.info("该用户已被禁用");
            return R.error("该用户已被禁用");
        }
        if (result == null) {
            return R.error("用户不存在");
        }
        Employee data = employeeMapper.login(employee);
        if (data != null) {
            //生成token（JWT）
            String token = JwtUtil.getToken(String.valueOf(data.getId()), data.getUsername());
            data.setToken(token);
            //登录成功后存到redis中
            redisTemplate.opsForValue().set(token, data.getUsername(), 5, TimeUnit.HOURS);
            return R.success(data);
        } else {
            return R.error("用户名或密码错误");
        }
    }

    /**
     * 添加员工
     *
     * @param employee
     * @return
     */
    @Override
    public R insertEmployee(Employee employee) {
        Employee result = employeeMapper.selectStatus(employee);
        if (result != null) {
            return R.error(result.getUsername() + "已存在");
        }
        int flags = employeeMapper.insertEmployee(employee);
        if (flags > 0) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    /**
     * 分页查询员工
     *
     * @param name
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public R showEmployee(String name, int page, int pagesize) {

//        没有输入名字，默认查询全部
        if (name == null || name == "") {
//          //从0索引开始查找
            int offset = PageUitl.getOffset(page, pagesize);
            List<Employee> employees = employeeMapper.showEmployee(offset, pagesize);
            System.out.println("员工分页查询：" + employees);
            int total = employeeMapper.selectCount();

            Page p = new Page();
            p.setCurrent(page);
            p.setTotal(total);
            p.setSize(pagesize);
            p.setRecords(employees);

            if (employees.size() >= 0) {
                return R.success(p);
            } else {
                return R.error("没有查询到任何信息");
            }
        } else {
//            否则按名字查找
            int offset = PageUitl.getOffset(page, pagesize);
            List<Employee> employee2 = employeeMapper.selectEmployeeByName(name, offset, pagesize);
            int total = employeeMapper.selectCountByName(name);

            Page p = new Page();
            p.setCurrent(page);
            p.setTotal(total);
            p.setSize(pagesize);
            p.setRecords(employee2);
            if (employee2.size() >= 0) {
                return R.success(p);
            } else {
                return R.error("没有查询到任何信息");
            }
        }
    }

    /**
     * 按照id查询员工
     *
     * @param id
     * @return
     */
    @Override
    public R selectEmployeeById(Integer id) {
        Employee employee = employeeMapper.selectEmployeeById(id);
        System.out.println("按照id查询员工:" + employee);
        if (employee != null) {
            return R.success(employee);
        } else {
            return R.error("查询员工信息出错");
        }
    }

    /**
     * 启用/禁用/修改员工信息
     *
     * @param employee
     * @return
     */
    @Override
    public R updateEmployee(Employee employee) {
//        查询用户命是否重复
//        Employee result = employeeMapper.selectStatus(employee);
//        if (result!=null){
//            return R.error(result.getUsername()+"已存在");
//        }
        int i = employeeMapper.updateEmployee(employee);
        if (i > 0) {
            return R.success("员工信息修改成功");
        } else {
            return R.error("员工信息修改失败");
        }
    }
}
